package com.qijia.travel.app;

import java.math.BigDecimal;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.qijia.travel.common.SqlMappedId;
import com.qijia.travel.dao.BaseDAO;
import com.qijia.travel.dao.SingletonDaoFactory;
import com.qijia.travel.dao.TravelDbException;
import com.qijia.travel.domain.PageResult;
import com.qijia.travel.domain.RewardProfit;
import com.qijia.travel.domain.StatisticsPojo;
import com.qijia.travel.domain.TouristGroupReward;
import com.qijia.travel.domain.example.ExampleFactory;
import com.qijia.travel.domain.example.TouristGroupRewardExample;

public class RewardManager {
	private RewardCounter rewardCounter;
	private BaseDAO baseDao;

	public RewardManager() {
		try {
			baseDao = SingletonDaoFactory.createDAO();
		} catch (TravelDbException e) {
			e.printStackTrace();
		}
	}

	public TouristGroupReward getTouristGroupRewardById(int touristGroupRewardId)
			throws AppErrorException {
		TouristGroupReward touristGroupReward = new TouristGroupReward();
		touristGroupReward.setTouristGroupRewardId(touristGroupRewardId);

		try {
			return (TouristGroupReward) baseDao.selectByPrimaryKey(
					SqlMappedId.TOURIST_GROUP_REWARD + "."
							+ SqlMappedId.selectByPrimaryKey,
					touristGroupReward);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取奖励信息失败!");
		}
	}

	public void createTouristGroupReward(TouristGroupReward touristGroupReward,
			HttpSession session) throws AppErrorException {
		if (touristGroupReward == null)
			throw new AppErrorException("请输入新增的奖励信息");
		try {
			if (touristGroupReward.getRewardAmount() != null)
				baseDao.insert(SqlMappedId.TOURIST_GROUP_REWARD + "."
						+ SqlMappedId.insert, touristGroupReward);
		} catch (TravelDbException e) {
			throw new AppErrorException("新增奖励失败,请稍候重试!", e);
		}
	}

	public void updateTouristGroupReward(TouristGroupReward touristGroupReward,
			HttpSession session) throws AppErrorException {
		if (touristGroupReward == null)
			throw new AppErrorException("请选择要进行修改的旅程信息");
		try {
			if (touristGroupReward.getTouristGroupRewardId() == null) {//若不存在则新建
				createTouristGroupReward(touristGroupReward, session);
			} else
				baseDao.updateByPrimaryKeySelective(
						SqlMappedId.TOURIST_GROUP_REWARD + "."
								+ SqlMappedId.updateByPrimaryKeySelective,
						touristGroupReward);
		} catch (TravelDbException e) {
			e.printStackTrace();
			throw new AppErrorException("更新选中的旅程["
					+ touristGroupReward.getTouristGroupDetailId()
					+ "]失败,请稍候重试!", e);
		}
	}

	public void deleteTouristGroupReward(int touristGroupRewardId)
			throws AppErrorException {
		if (touristGroupRewardId == 0)
			throw new AppErrorException("请选择要删除的旅程奖励信息");
		TouristGroupReward touristGroupReward = new TouristGroupReward();
		try {
			touristGroupReward.setTouristGroupRewardId(touristGroupRewardId);
			baseDao.deleteByPrimaryKey(SqlMappedId.TOURIST_GROUP_REWARD + "."
					+ SqlMappedId.deleteByPrimaryKey, touristGroupReward);
		} catch (TravelDbException e) {
			throw new AppErrorException("删除选中的旅程奖励信息[编号为:"
					+ touristGroupReward.getTouristGroupRewardId()
					+ "]失败,请稍候重试!", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<TouristGroupReward> getTouristGroupRewardByExample()
			throws AppErrorException {
		TouristGroupRewardExample example = ExampleFactory
				.createTouristGroupRewardExample();
		example.createCriteria();
		try {
			return (List<TouristGroupReward>) baseDao.selectByExample(
					SqlMappedId.TOURIST_GROUP_REWARD + "."
							+ SqlMappedId.selectByExample, example);
		} catch (TravelDbException e) {
			throw new AppErrorException("查询指定条件下的奖励信息失败,请稍候重试!", e);
		}
	}

	public PageResult selectTouristGroupRewardList(int curPage)
			throws AppErrorException {
		TouristGroupRewardExample example = ExampleFactory
				.createTouristGroupRewardExample();
		example.createCriteria();
		try {
			PageResult page = PageResult.newInstance();
			page.setSelectDataMappedId(SqlMappedId.TOURIST_GROUP_REWARD + "."
					+ SqlMappedId.selectByExample);
			page.setCountMappedId(SqlMappedId.TOURIST_GROUP_REWARD + "."
					+ SqlMappedId.countByExample);
			page.setSqlParamObj(example);

			if (curPage <= 1)
				curPage = 1;

			page.setCurPage(curPage);
			page.setPageSize(10);

			return baseDao.selectByPage(page);
		} catch (TravelDbException e) {
			throw new AppErrorException("获取奖励数据列表失败,请稍候重试!", e);
		} catch (InstantiationException e) {
			throw new AppErrorException("获取奖励数据列表失败,请稍候重试!", e);
		} catch (IllegalAccessException e) {
			throw new AppErrorException("获取奖励数据列表失败,请稍候重试!", e);
		}
	}

	public BigDecimal countReward(StatisticsPojo detail,
			RewardProfit rewardProfit) {
		return this.rewardCounter.countReward(detail, rewardProfit);
	}

	public RewardCounter getRewardCounter() {
		return rewardCounter;
	}

	public void setRewardCounter(RewardCounter rewardCounter) {
		this.rewardCounter = rewardCounter;
	}
}
